#include "Game.hh" // 包含游戏头文件，定义了Game类和相关功能 / Include Game header, defines Game class and related functions
#include <cstdlib> // 包含标准库，用于生成随机数 / Include standard library for generating random numbers
#include <vector> // 包含向量库，用于存储生物群系描述 / Include vector library for storing biome descriptions

// 获取房间描述：根据玩家坐标(x,y)返回一个生物群系的描述
// Get room description: Return a biome description based on player's coordinates (x,y)
std::string Game::getRoomDescription(int x, int y) {
    std::vector<std::string> biomes = { // 定义一个生物群系列表，包含不同地形的描述 / Define a list of biomes with descriptions of different terrains
        "Plains: Vast grasslands with gentle breezes.", // 平原：广阔的草地，微风吹过 / Plains: Vast grasslands with gentle breezes
        "Forest: Dense trees where monsters may lurk.", // 森林：茂密的树林，可能潜伏着怪兽 / Forest: Dense trees where monsters may lurk
        "Desert: Scorching sands hiding dangers.", // 沙漠：炽热的沙地，隐藏着危险 / Desert: Scorching sands hiding dangers
        "Mountains: Towering peaks full of peril.", // 山脉：高耸的山峰，充满危险 / Mountains: Towering peaks full of peril
        "Swamp: Misty wetlands with eerie sounds." // 沼泽：雾气弥漫的湿地，传来诡异的声音 / Swamp: Misty wetlands with eerie sounds
    };
    return biomes[(x * 31 + y * 17) % biomes.size()]; // 使用公式计算索引，选择一个生物群系描述 / Use formula to calculate index, select a biome description
    // 公式(x * 31 + y * 17) % biomes.size()确保不同坐标生成不同但可预测的描述 / Formula (x * 31 + y * 17) % biomes.size() ensures different but predictable descriptions for coordinates
}

// 生成敌人：在当前房间随机添加敌人
// Spawn enemies: Randomly add enemies to the current room
void Game::spawnEnemies() {
    if (currentRoom == rooms[0] || inVillage) return; // 如果在The End或村庄，不生成敌人，直接退出 / If in The End or village, don't spawn enemies, exit function

    auto& enemies = currentRoom->getEnemies(); // 获取当前房间的敌人列表 / Get the list of enemies in the current room
    enemies.clear(); // 清空当前房间的敌人列表，准备生成新敌人 / Clear current room's enemy list to prepare for new enemies
    int spawnChance = rand() % 3; // 随机生成0到2的数字，决定是否生成敌人 / Generate random number 0-2 to decide if enemies spawn
    if (spawnChance == 0) { // 如果随机数为0（1/3概率），生成敌人 / If random number is 0 (1/3 chance), spawn enemies
        int enemyType = rand() % 4; // 随机选择0到3，决定生成哪种敌人 / Randomly choose 0-3 to decide which enemy to spawn
        switch (enemyType) { // 根据敌人类型生成不同敌人 / Spawn different enemies based on type
            case 0: 
                enemies.push_back(new Character("Creeper", "An explosive green monster", 30, 30, 20, 2)); // 添加Creeper：爆炸绿色怪兽 / Add Creeper: Explosive green monster
                addMessage("[Turn " + std::to_string(turnCount) + "] A Creeper appeared!"); // 添加消息：Creeper出现 / Add message: Creeper appeared
                break; // 结束此选项 / End this case
            case 1: 
                enemies.push_back(new Character("Skeleton", "An archer skeleton", 40, 40, 10, 3)); // 添加Skeleton：弓箭手骷髅 / Add Skeleton: Archer skeleton
                addMessage("[Turn " + std::to_string(turnCount) + "] A Skeleton appeared!"); // 添加消息：Skeleton出现 / Add message: Skeleton appeared
                break; // 结束此选项 / End this case
            case 2: 
                enemies.push_back(new Character("Pillager", "A mountain bandit", 50, 50, 12, 4)); // 添加Pillager：山地强盗 / Add Pillager: Mountain bandit
                addMessage("[Turn " + std::to_string(turnCount) + "] A Pillager appeared!"); // 添加消息：Pillager出现 / Add message: Pillager appeared
                break; // 结束此选项 / End this case
            case 3: 
                enemies.push_back(new Character("Witch", "A swamp sorceress", 60, 60, 10, 5)); // 添加Witch：沼泽女巫 / Add Witch: Swamp sorceress
                addMessage("[Turn " + std::to_string(turnCount) + "] A Witch appeared!"); // 添加消息：Witch出现 / Add message: Witch appeared
                break; // 结束此选项 / End this case
        }
        if (rand() % 2 == 0) { // 以50%概率额外生成一个Zombie / 50% chance to spawn an additional Zombie
            enemies.push_back(new Character("Zombie", "A rotting monster", 30, 30, 8, 2)); // 添加Zombie：腐烂怪兽 / Add Zombie: Rotting monster
            addMessage("[Turn " + std::to_string(turnCount) + "] A Zombie appeared too!"); // 添加消息：Zombie也出现了 / Add message: Zombie appeared too
        }
    }
}

// 触发随机事件：在当前房间随机发生特殊事件
// Trigger random event: Randomly trigger a special event in the current room
void Game::triggerRandomEvent() {
    if (currentRoom == rooms[0] || inVillage) return; // 如果在The End或村庄，不触发事件，直接退出 / If in The End or village, don't trigger events, exit function

    int event = rand() % 10; // 随机生成0到9的数字，决定触发的事件 / Generate random number 0-9 to decide the event
    if (event == 0) { // 如果随机数为0（10%概率），进入村庄 / If random number is 0 (10% chance), enter village
        addMessage("[Turn " + std::to_string(turnCount) + "] A villager greets you!"); // 添加消息：村民迎接你 / Add message: Villager greets you
        inVillage = true; // 设置玩家进入村庄 / Set player as in village
    } else if (event == 1) { // 如果随机数为1（10%概率），被小僵尸伏击 / If random number is 1 (10% chance), ambushed by baby zombies
        addMessage("[Turn " + std::to_string(turnCount) + "] You are ambushed by baby zombies!"); // 添加消息：被小僵尸伏击 / Add message: Ambushed by baby zombies
        currentRoom->addEnemy(new Character("Baby Zombie", "A small rotting monster", 20, 20, 5, 1)); // 添加小僵尸敌人 / Add Baby Zombie enemy
        if (player->takeDamage(10) && !player->isAlive()) { // 玩家受到10点伤害，检查是否死亡 / Player takes 10 damage, check if dead
            addMessage("[Turn " + std::to_string(turnCount) + "] You died! Game Over!"); // 添加消息：玩家死亡，游戏结束 / Add message: Player died, game over
            gameOver = true; // 设置游戏结束 / Set game over
        }
    } else if (event >= 2 && event <= 3) { // 如果随机数为2或3（20%概率），找到物品 / If random number is 2 or 3 (20% chance), find an item
        int itemType = rand() % 5; // 随机选择0到4，决定找到的物品 / Randomly choose 0-4 to decide the item found
        switch (itemType) { // 根据物品类型添加不同物品 / Add different items based on type
            case 0: 
                currentRoom->addItem(new Item("Stick", "A simple stick", 3, 0, 0)); // 添加木棍（+3攻击） / Add Stick (+3 attack)
                addMessage("[Turn " + std::to_string(turnCount) + "] You found a Stick!"); // 添加消息：找到木棍 / Add message: Found a Stick
                break; // 结束此选项 / End this case
            case 1: 
                currentRoom->addItem(new Item("Bone", "A skeleton's drop", 0, 0, 0)); // 添加骨头（无加成） / Add Bone (no bonus)
                addMessage("[Turn " + std::to_string(turnCount) + "] You found a Bone!"); // 添加消息：找到骨头 / Add message: Found a Bone
                break; // 结束此选项 / End this case
            case 2: 
                currentRoom->addItem(new Item("Bread", "A villager's gift", 0, 0, 10)); // 添加面包（+10生命） / Add Bread (+10 health)
                addMessage("[Turn " + std::to_string(turnCount) + "] You found Bread!"); // 添加消息：找到面包 / Add message: Found Bread
                break; // 结束此选项 / End this case
            case 3: 
                currentRoom->addItem(new Item("Crossbow", "A pillager's weapon", 10, 0, 0)); // 添加弩（+10攻击） / Add Crossbow (+10 attack)
                addMessage("[Turn " + std::to_string(turnCount) + "] You found a Crossbow!"); // 添加消息：找到弩 / Add message: Found a Crossbow
                break; // 结束此选项 / End this case
            case 4: 
                currentRoom->addItem(new Item("Healing Potion", "Restores health", 0, 0, 50)); // 添加治疗药水（+50生命） / Add Healing Potion (+50 health)
                addMessage("[Turn " + std::to_string(turnCount) + "] You found a Healing Potion!"); // 添加消息：找到治疗药水 / Add message: Found a Healing Potion
                break; // 结束此选项 / End this case
        }
    }
    // 如果event为4-9（60%概率），什么都不发生 / If event is 4-9 (60% chance), nothing happens
}